#include "dialog.h"
#include <QPainter>
#include <QMouseEvent>
#include <QToolTip>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setMouseTracking(true);
    setGeometry(100, 100, 400, 300);
}

Dialog::~Dialog()
{

}

void Dialog::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    int side = qMin(width(), height());
    painter.setViewport(0, 0, side, side); // 如果不设置，画出来的将是长方形，而不是正方形

    painter.setWindow(-50, -50, 100, 100);
    painter.fillRect(-50, -50, 100, 100, Qt::SolidPattern);

    painter.setBrush(Qt::green);
    painter.drawRect(0, 0, 20, 20);
}

void Dialog::mouseMoveEvent(QMouseEvent *event)
{
    QString pos = QString("%1, %2").arg(event->pos().x()).arg(event->pos().y());
    QToolTip::showText(event->globalPos(), pos, this);
}
